﻿using System;
using System.Collections.Generic;
using olierFrm.WebFormDev;
using olierFrm.DataBase.FrameWork.StdQuery;
using System.Linq;
using olierFrm.WebFormDev.Session;
using olierFrm.WebFormDev.StdQuery;
using olierFrm.Extensions.EDataTable;

namespace WebFormDev.StdQueryPack
{
    public partial class DetailGridPage : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            ManageSystem.IsLogin();
            ManageSystem.SetTheme(Page);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            StdQuerySQL masterdbStdQuery = new StdQuerySQL(Request.QueryString["MasterQueryName"]);
            SubQuery subQuery = new SubQuery(masterdbStdQuery.StdQuery.SubQuery);
            var dbStdQuery = new StdQuerySQL(StdQuery.QueryName);
            List<string> HiddenFilds = new List<string>();
            dbStdQuery.AddSystemParams();
            foreach (var s in subQuery.SciptObject)
            {
                if (s.Value.HintQueryName != StdQuery.QueryName) continue;

                var subQueryInfo = subQuery.SciptObject[s.Key];
                var pms = subQueryInfo.ParamMatch.Split(';').ToList();
                foreach (var p in dbStdQuery.StdQueryParams)
                {
                    //寻找匹配参数
                    var pm = pms.Where(pp => pp.IndexOf(p.ParamName + "=") != -1).ToList();
                    if (pm.Count() != 0)
                    {
                        var ParamValue = Request.QueryString[pm.First().Split('=')[1]];
                        dbStdQuery.SetParam(p.ParamName, ParamValue);
                    }
                    else
                    {
                        var ParamValue = Request.QueryString[p.ParamName];
                        if (ParamValue != null)
                        {
                            dbStdQuery.SetParam(p.ParamName, ParamValue);
                        }
                    }
                }
                dbStdQuery.AddSystemParams();
                foreach (var p in dbStdQuery.StdQueryParams)
                {
                    if (Request.QueryString[p.ParamName] != null)
                        dbStdQuery.SetParam(p.ParamName, Request.QueryString[p.ParamName]);
                }
                HiddenFilds = s.Value.HideFields.Split(';').ToList();
                break;
            }



            //var su = (SessionUser)Session[SessionUser.SessionID];
            var dtSource = dbStdQuery.ExecuteSQL();
            dtSource.AddDataRowIndex();

            StdQuerySource sss = new StdQuerySource(dtSource, dbStdQuery);
            dtSource.AddHideColumn(HiddenFilds);
            dtSource.AddHideColumn(dbStdQuery.StdQuery.HideFields.Split(';').ToList());
            var gridDetail = sss.CreateGridView("DetailGrid", 10);
            //gridDetail.ClientSideEvents.RowClick = "function(s,e){DetailGrid.SelectAllRowsOnPage(false);DetailGrid.SelectRow(e.visibleIndex, true);}";
            //gridDetail.ClientSideEvents.EndCallback = "function(s,e){$(self.frameElement).height($(\"#DetailGrid\").height());InitGrid();}";
            //if (dtSource.Rows.Count != 0)
            //    gridDetail.Settings.ShowVerticalScrollBar = true;

            //gridDetail.ID = "DetailGrid";

            //gridDetail.DataSource = dtSource;
            //gridDetail.SettingsPager.PageSize = 10;
            //gridDetail.DataBind();


            //gridDetail.SettingsBehavior.
            //gridDetail.VisibleRowCount * 33;
            //gridDetail.AutoWidth();


            //gridDetail.Width = new Unit("100%");
            //gridDetail.Width = 700;
            
            form1.Controls.Add(gridDetail);
        }


    }
}